package com.manage.demo.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.manage.demo.dao.model.ExamResults;
import com.manage.demo.service.MangeintoService;

@Controller
public class MangeintoController {

	@Autowired
	private MangeintoService MangeintoService;

	@RequestMapping(value="/mangeinto", method=RequestMethod.GET)
	public String mangeinto(HttpServletRequest request, Model model){
		System.out.println(request.getSession().getAttribute("perm"));
		if((request.getSession().getAttribute("perm")).toString().equals("0")) {
			return "/html/mangeintostu";
		}else {
			SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM");
			Date date = new Date(System.currentTimeMillis());
			String datestr = formatter.format(date);
			System.out.println(datestr);
			String year = datestr.substring(0, 4);
			String month = datestr.substring(5, 7);
			String term = "";
			System.out.println(year);
			System.out.println(month);
			if(Integer.parseInt(month) < 7) {
				term = year + "年春季";
			}else {
				term = year + "年秋季";
			}
			String semester = "本学期为" + term + "学期！";
			String tch_course = "";
			if(request.getSession().getAttribute("tch_course") != null) {
				tch_course = request.getSession().getAttribute("tch_course").toString();
			}
			model.addAttribute("semester", semester);
			model.addAttribute("perm", request.getSession().getAttribute("perm"));
	        model.addAttribute("name", request.getSession().getAttribute("name"));
			model.addAttribute("tch_course", tch_course);
			model.addAttribute("stu_id11", MangeintoService.getStu11());
			model.addAttribute("stu_id12", MangeintoService.getStu12());
			model.addAttribute("stu_id13", MangeintoService.getStu13());
			model.addAttribute("stu_id14", MangeintoService.getStu14());
			model.addAttribute("stu_id15", MangeintoService.getStu15());
			model.addAttribute("stu_id21", MangeintoService.getStu21());
			model.addAttribute("stu_id22", MangeintoService.getStu22());
			model.addAttribute("stu_id23", MangeintoService.getStu23());
			model.addAttribute("stu_id24", MangeintoService.getStu24());
			model.addAttribute("stu_id25", MangeintoService.getStu25());
			model.addAttribute("stu_id31", MangeintoService.getStu31());
			model.addAttribute("stu_id32", MangeintoService.getStu32());
			model.addAttribute("stu_id33", MangeintoService.getStu33());
			model.addAttribute("stu_id34", MangeintoService.getStu34());
			model.addAttribute("stu_id35", MangeintoService.getStu35());
			String id = request.getParameter("id");
			if(id != null && id != "") {
				ExamResults getresult = MangeintoService.getResult(id);
				String examtype = getresult.getExam_type();
				String stu_id = getresult.getStu_id();
				String getterm = getresult.getTerm();
				String result = "";
				if(tch_course.equals("语文")) {
					result = getresult.getChinese().toString();
				}
				else if(tch_course.equals("数学")) {
					result = getresult.getMath().toString();
				}
				else if(tch_course.equals("英语")) {
					result = getresult.getEnglish().toString();
				}
				else if(tch_course.equals("物理")) {
					result = getresult.getPhysics().toString();
				}
				else if(tch_course.equals("化学")) {
					result = getresult.getChemistry().toString();
				}
				model.addAttribute("statue", "change");
				model.addAttribute("examtype", examtype);
				model.addAttribute("stu_id", stu_id);
				model.addAttribute("result", result);
				model.addAttribute("getterm", getterm);
			}
			else {
				model.addAttribute("statue", null);
			}
		    return "/html/mangeinto";
		}
	}

	@RequestMapping(value="/mangeinto", method=RequestMethod.POST)
	public String mangeinto2(HttpServletRequest request, Model model){
		SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM");
		Date date = new Date(System.currentTimeMillis());
		String datestr = formatter.format(date);
		System.out.println(datestr);
		String year = datestr.substring(0, 4);
		String month = datestr.substring(5, 7);
		String term = "";
		System.out.println(year);
		System.out.println(month);
		if(Integer.parseInt(month) < 7) {
			term = year + "年春季";
		}else {
			term = year + "年秋季";
		}
		String semester = "本学期为" + term + "学期！";
		model.addAttribute("semester", semester);
		
		String tch_course = request.getSession().getAttribute("tch_course").toString();
		System.out.println(tch_course);
		System.out.println(tch_course.getClass());
		model.addAttribute("perm", request.getSession().getAttribute("perm"));
        model.addAttribute("name", request.getSession().getAttribute("name"));
		model.addAttribute("tch_course", tch_course);
		
		String examtype = request.getParameter("examtype");
		String stu_id = request.getParameter("stu_id");
		String result = request.getParameter("result");
		String gradeclass = request.getParameter("gradeclass");
		
		model.addAttribute("examtype", examtype);
		model.addAttribute("stu_id", stu_id);
		model.addAttribute("result", result);
		model.addAttribute("gradeclass", gradeclass);
		model.addAttribute("stu_id11", MangeintoService.getStu11());
		model.addAttribute("stu_id12", MangeintoService.getStu12());
		model.addAttribute("stu_id13", MangeintoService.getStu13());
		model.addAttribute("stu_id14", MangeintoService.getStu14());
		model.addAttribute("stu_id15", MangeintoService.getStu15());
		model.addAttribute("stu_id21", MangeintoService.getStu21());
		model.addAttribute("stu_id22", MangeintoService.getStu22());
		model.addAttribute("stu_id23", MangeintoService.getStu23());
		model.addAttribute("stu_id24", MangeintoService.getStu24());
		model.addAttribute("stu_id25", MangeintoService.getStu25());
		model.addAttribute("stu_id31", MangeintoService.getStu31());
		model.addAttribute("stu_id32", MangeintoService.getStu32());
		model.addAttribute("stu_id33", MangeintoService.getStu33());
		model.addAttribute("stu_id34", MangeintoService.getStu34());
		model.addAttribute("stu_id35", MangeintoService.getStu35());
		
		String id = request.getParameter("id");
		if(id != null && id != "") {
			model.addAttribute("statue", "change");
			term = request.getParameter("getterm");
			if(examtype.equals("") || stu_id.equals("") || result.equals("")) {
				if(examtype.equals("")) {
					model.addAttribute("code", "200");
					model.addAttribute("msg", "请选择考试类别");
				}
				else if(stu_id.equals("")) {
					model.addAttribute("code", "200");
					model.addAttribute("msg", "请输入学号");
				}
				else if(result.equals("")) {
					model.addAttribute("code", "200");
					model.addAttribute("msg", "请输入成绩");
				}
			}
			else {
				if(tch_course.equals("语文")) {
					System.out.println("现在是语文老师");
					MangeintoService.updateChinsesExamResults(result, stu_id, term, examtype);
					System.out.println("修改成功");
					model.addAttribute("code", "500");
					model.addAttribute("msg", "修改语文成绩成功！");
				}
				else if(tch_course.equals("数学")) {
					MangeintoService.updateMathExamResults(result, stu_id, term, examtype);
					System.out.println("修改成功");
					model.addAttribute("code", "500");
					model.addAttribute("msg", "修改数学成绩成功！");
				}
				else if(tch_course.equals("英语")) {
					MangeintoService.updateEnglishExamResults(result, stu_id, term, examtype);
					System.out.println("修改成功");
					model.addAttribute("code", "500");
					model.addAttribute("msg", "修改英语成绩成功！");
				}
				else if(tch_course.equals("物理")) {
					MangeintoService.updatePhysicsExamResults(result, stu_id, term, examtype);
					System.out.println("修改成功");
					model.addAttribute("code", "500");
					model.addAttribute("msg", "修改物理成绩成功！");
				}
				else if(tch_course.equals("化学")) {
					MangeintoService.updateChemistryExamResults(result, stu_id, term, examtype);
					System.out.println("修改成功");
					model.addAttribute("code", "500");
					model.addAttribute("msg", "修改化学成绩成功！");
				}
			}
		}
		else{
			model.addAttribute("statue", null);
			if(examtype.equals("") || stu_id.equals("") || result.equals("")) {
				if(examtype.equals("")) {
					model.addAttribute("code", "200");
					model.addAttribute("msg", "请选择考试类别");
				}
				else if(stu_id.equals("")) {
					model.addAttribute("code", "200");
					model.addAttribute("msg", "请输入学号");
				}
				else if(result.equals("")) {
					model.addAttribute("code", "200");
					model.addAttribute("msg", "请输入成绩");
				}
			}
			else {
				List<ExamResults> resultLst = MangeintoService.getExamResults(stu_id, term, examtype);
				if(resultLst.size() != 0) {
					System.out.println("已有成绩");
					if(tch_course.equals("语文")) {
						System.out.println("现在是语文老师");
						if(resultLst.get(0).getChinese() != null) {
							System.out.println("已有语文成绩");
							model.addAttribute("code", "200");
							model.addAttribute("msg", "已存在该次考试成绩，请从成绩查询页面进入并修改！");
						}
						else {
							MangeintoService.updateChinsesExamResults(result, stu_id, term, examtype);
							System.out.println("录入成功");
							model.addAttribute("code", "500");
							model.addAttribute("msg", "录入语文成绩成功！");
						}
					}
					else if(tch_course.equals("数学")) {
						if(resultLst.get(0).getMath() != null) {
							System.out.println("已有数学成绩");
							model.addAttribute("code", "200");
							model.addAttribute("msg", "已存在该次考试成绩，请从成绩查询页面进入并修改！");
						}
						else {
							MangeintoService.updateMathExamResults(result, stu_id, term, examtype);
							System.out.println("录入成功");
							model.addAttribute("code", "500");
							model.addAttribute("msg", "录入数学成绩成功！");
						}
					}
					else if(tch_course.equals("英语")) {
						if(resultLst.get(0).getEnglish() != null) {
							System.out.println("已有英语成绩");
							model.addAttribute("code", "200");
							model.addAttribute("msg", "已存在该次考试成绩，请从成绩查询页面进入并修改！");
						}
						else {
							MangeintoService.updateEnglishExamResults(result, stu_id, term, examtype);
							System.out.println("录入成功");
							model.addAttribute("code", "500");
							model.addAttribute("msg", "录入英语成绩成功！");
						}
					}
					else if(tch_course.equals("物理")) {
						if(resultLst.get(0).getPhysics() != null) {
							System.out.println("已有物理成绩");
							model.addAttribute("code", "200");
							model.addAttribute("msg", "已存在该次考试成绩，请从成绩查询页面进入并修改！");
						}
						else {
							MangeintoService.updatePhysicsExamResults(result, stu_id, term, examtype);
							System.out.println("录入成功");
							model.addAttribute("code", "500");
							model.addAttribute("msg", "录入物理成绩成功！");
						}
					}
					else if(tch_course.equals("化学")) {
						if(resultLst.get(0).getChemistry() != null) {
							System.out.println("已有化学成绩");
							model.addAttribute("code", "200");
							model.addAttribute("msg", "已存在该次考试成绩，请从成绩查询页面进入并修改！");
						}
						else {
							MangeintoService.updateChemistryExamResults(result, stu_id, term, examtype);
							System.out.println("录入成功");
							model.addAttribute("code", "500");
							model.addAttribute("msg", "录入化学成绩成功！");
						}
					}
				}
				else {
					MangeintoService.insertExamResults(stu_id, term, examtype);
					if(tch_course.equals("语文")) {
						MangeintoService.updateChinsesExamResults(result, stu_id, term, examtype);
						System.out.println("录入成功");
						model.addAttribute("code", "500");
						model.addAttribute("msg", "录入语文成绩成功！");
					}
					else if(tch_course.equals("数学")) {
						MangeintoService.updateMathExamResults(result, stu_id, term, examtype);
						System.out.println("录入成功");
						model.addAttribute("code", "500");
						model.addAttribute("msg", "录入数学成绩成功！");
					}
					else if(tch_course.equals("英语")) {
						MangeintoService.updateEnglishExamResults(result, stu_id, term, examtype);
						System.out.println("录入成功");
						model.addAttribute("code", "500");
						model.addAttribute("msg", "录入英语成绩成功！");
					}
					else if(tch_course.equals("物理")) {
						MangeintoService.updatePhysicsExamResults(result, stu_id, term, examtype);
						System.out.println("录入成功");
						model.addAttribute("code", "500");
						model.addAttribute("msg", "录入物理成绩成功！");
					}
					else if(tch_course.equals("化学")) {
						MangeintoService.updateChemistryExamResults(result, stu_id, term, examtype);
						System.out.println("录入成功");
						model.addAttribute("code", "500");
						model.addAttribute("msg", "录入化学成绩成功！");
					}
				}
			}
		}
		
		return "/html/mangeinto";
	}

}
